package com.clotingshoppos.modules.product.service.serviceImpl;

import com.clotingshoppos.modules.product.entity.ProductDataDictionaryEntity;
import com.clotingshoppos.modules.product.repository.ProductDDRepository;
import com.clotingshoppos.modules.product.repository.ProductPropertyRepository;
import com.clotingshoppos.modules.product.service.ProductDDService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @ClassName: ProductDDServiceImp
 * @Description: 产品数据字典服务层接口实现类
 * @Author: zhandad
 * @Date: 2019-03-15 11:21
 * @Version: v1.0
 */
@Service
public class ProductDDServiceImp implements ProductDDService {
    @Autowired
    ProductDDRepository productDDRepository;
    @Autowired
    ProductPropertyRepository productPropertyRepository;

    @Override
    /**
    * @Description: 增加产品属性
    * @Param: [productDataDictionaryEntity]
    * @return: ProductDataDictionaryEntity
    * @Author: zhandad
    * @Date: 2019/3/16  22:57
    */
    public ProductDataDictionaryEntity addtoPDD(ProductDataDictionaryEntity productDataDictionaryEntity) {
        return productDDRepository.save(productDataDictionaryEntity);
    }

    @Override
    /**
    * @Description: 删除一个属性
    * @Param: [id]
    * @return: int
    * @Author: zhandad
    * @Date: 2019/3/16  22:58
    */
    public ProductDataDictionaryEntity delete(ProductDataDictionaryEntity productDataDictionaryEntity) {
         productDDRepository.delete(productDataDictionaryEntity);
        return productDataDictionaryEntity;
    }

    @Override
    /**
    * @Description: 按照属性类型列出可选项
    * @Param: [type]
    * @return: ProductDataDictionaryEntity
    * @Author: zhandad
    * @Date: 2019/3/16  22:59
    */
    public List<ProductDataDictionaryEntity> findByForm(String form) {
        return productDDRepository.findByForm(form);
    }

    @Override
    /**
    * @Description: 按照id查找某一具体属性
    * @Param: [id]
    * @return: ProductDataDictionaryEntity
    * @Author: zhandad
    * @Date: 2019/3/16  22:59
    */
    public Optional<ProductDataDictionaryEntity> findByPDDId(String id) {
        ProductDataDictionaryEntity productDataDictionaryEntity = productDDRepository.findById(id).get();
        productDataDictionaryEntity.setForm(productPropertyRepository.findById(productDataDictionaryEntity.getForm()).get().getPropertyValue());
        return Optional.ofNullable(productDataDictionaryEntity);
    }

    @Override
    /**
    * @Description: 更新某一具体属性
    * @Param: [productDataDictionaryEntity]
    * @return: ProductDataDictionaryEntity
    * @Author: zhandad
    * @Date: 2019/3/16  23:00
    */
    public ProductDataDictionaryEntity update(ProductDataDictionaryEntity productDataDictionaryEntity) {
        return productDDRepository.save(productDataDictionaryEntity);
    }
}
